<!DOCTYPE html>
<html>
<#include "_inc/constants.ftl" />
<head>
    <title></title>
    <!-- For-Mobile-Apps-and-Meta-Tags -->
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=false;" name="viewport" />
    <meta name="keywords"
          content="Fortune Estates Widget Responsive, Login Form Web Template, Flat Pricing Tables, Flat Drop-Downs, Sign-Up Web Templates, Flat Web Templates, Login Sign-up Responsive Web Template, SmartPhone Compatible Web Template, Free Web Designs for Nokia, Samsung, LG, Sony Ericsson, Motorola Web Design"/>
    <link href="css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
    <link rel="stylesheet" href="css/ken-burns.css" type="text/css" media="all"/>
    <!--//pop-up-box -->
    <!-- web-fonts -->
    <link href='http://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic'
          rel='stylesheet' type='text/css'>
    <!-- //web-fonts -->
    <link rel="stylesheet" href="css/weui.min.css">
    <link rel="stylesheet" href="css/jquery-weui.css">
    <link rel="stylesheet" href="css/demos.css">
    <link rel="stylesheet" href="css/common.css">
    <style>
        body {
            width: 100%;
            margin: 0 auto;
        }

        p {
            margin: 0 0 0;
        }

        h1 {
            margin: 0 0 0;
        }

        .price {
            margin-top: 21px;
            font-size: 44px;
        }


        /*媒体查询：注意and后面空格的添加*/
        /*iphone: w < 768px*/

        @media screen and (max-width: 375px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 16px;
                font-family: "微软雅黑";
            }

            .price {
                margin-top: 3px;
                font-size: 14px;
            }
        }

        @media screen and (max-width: 425px) and (min-width: 376px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 21px;
            }

            .price {
                margin-top: 6px;
                font-size: 19px;
            }
        }

        @media screen and (max-width: 597px) and (min-width: 426px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 25px;
            }

            .price {
                margin-top: 9px;
                font-size: 24px;
            }
        }

        @media screen and (max-width: 768px) and (min-width: 597px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 30px;
            }

            .price {
                margin-top: 12px;
                font-size: 29px;
            }
        }

        /*pad: w >= 768  && w< 992*/

        @media screen and (max-width: 992px) and (min-width: 767px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 35px;
            }

            .price {
                margin-top: 15px;
                font-size: 34px;
            }
        }

        /*中等屏幕   w >= 992  && w<1200*/

        @media screen and (max-width: 1200px) and (min-width: 992px) {
            body {
                width: 100%;
            }

            .name {
                font-size: 40px;
            }

            .price {
                margin-top: 18px;
                font-size: 39px;
            }
        }

        .weui-cells {
            font-size: 15px;
        }


    </style>
</head>

<body class="bg" style="background-color: #FFFBE5;">
<div class="agile-main">
    <div>
        <img src="images/ttt.png" style="width: 100%; height: 40px;">
    </div>
    <div class="content-wrap">
        <div class="content" style="padding-left: 20px;padding-right: 20px;margin-top: 10px;">

            <div id="prompt" style="margin-top: 10px;display: none;text-align: center;width: 100%;">
                您还没有订单哦！
            </div>

            <div id="orderDetail" style="margin-top: 20px;">

            </div>
            <div id="counts" style="text-align: center;"></div>
        </div>
        <div style="height: 100px;"></div>
        <#include "_inc/menufooter.ftl" />
    </div>
</div>

<!-- js -->
<script src="js/jquery.min.js"></script>
<!-- //js -->
<script src="js/jquery.magnific-popup.js" type="text/javascript"></script>
<script src="js/classie.js"></script>
<script src="js/jquery.nicescroll.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/fastclick.js"></script>
<script src="js/jquery-weui.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="js/utils.js"></script>
<script>

    wx.config({
        debug: false,
        appId: '${appid}',
        timestamp: ${timestamp},
        nonceStr: '${noncestr}',
        signature: '${signature}',
        jsApiList: [
            'chooseWXPay'
        ]
    });

    wx.error(function(res){
        alert(res.errMsg);
    });



    $(function(){
        var name = localStorage.getItem("pagename");
        if(name == "paypage") {
            localStorage.removeItem("pagename");
            pushHistory();
            window.addEventListener("popstate", function(e) {  //回调函数中实现需要的功能
                var param = {};
                param.openid = '${openid}';
                param.accesstoken = '${accesstoken}';
                param.state = '${mach}';
                $.submitFormPost('/weixin.do', param, 'POST');
            }, false);
        }
    });
    function pushHistory() {
        var state = {
            title: "title",
            url: "__SELF__"
        };
        window.history.pushState(state, state.title, state.url);
    }
    $(document).ready(function () {

        var param = {};
        param.openid = '${openid}';
        param.accesstoken = '${accesstoken}';
        //获取订单列表
        $.ajax({
            type: 'POST',
            url: '${url}/order/myorder.do',
            cache: false,
            dataType: 'json',
            data: param,
            success: function (result) {
                if (result.code == 0) {
                    var orderlist = result.orderlist;
                    var orderid = "";
                    if (orderlist.length == 0) {
                        alert("您还没有订单");
                        $("#counts").html("您还没有订单噢！");
                    } else {
                        $("#counts").html("共" +orderlist.length+ "条记录");
                        for (var i = 0; i < orderlist.length; i++) {
                            orderid = orderlist[i].id;
                            console.log(orderid);
                            $('#orderDetail').before(function () {
                                var div = '<div style="margin-top:10px;">' +
                                        '<div class="weui-flex" style="background-color:white; margin-top: 10px;">' +
                                        '<div class="weui-flex__item">' +
                                        '<div class="weui-cells__title">订单编号：' + orderid + '</div>' +
                                        '<div class="weui-cells">';

                                var detailList = orderlist[i].detail;
                                for (var j = 0; j < detailList.length; j++) {
                                    div += '<div class="weui-cell">' +
                                            '<div class="weui-cell__hd"><img src="images/teaico.jpg" alt="" style="width:20px;margin-right:5px;display:block"></div>' +
                                            '<div class="weui-cell__bd">' +
                                            '<p>' + detailList[j].productname + '</p>' +
                                            '</div>' +
                                            '<div class="weui-cell__ft">x' + detailList[j].counts + '</div>' +
                                            '</div>';
                                    div += '<div style="display: none;" class="price'+orderid+'">' +
                                            '<input type="text" class="counts" value="'+detailList[j].counts+'"/>' +
                                            '<input type="text" class="cprice" value="'+ detailList[j].cprice +'"/>' +
                                            '<input type="text" class="price" value="'+detailList[j].price+'"/>' +
                                            '<input type="text" class="pid" value="'+detailList[j].pid+'"/>' +
                                            '<input type="text" class="productname" value="'+detailList[j].productname+'"/>' +
                                            '<input type="text" class="description" value="'+detailList[j].description+'"/>' +
                                            '</div>';
                                }

                                if (orderlist[i].status == 4) {
                                    div += '<div class="weui-cell weui-cell_link">' +
                                            '<div class="weui-cell__bd" style="color: #0bb20c;">已支付</div> ' +
                                            '<div class="weui-cell__ft">共 ' + orderlist[i].total + ' 杯，实付 ' + orderlist[i].acprice + '</div>' +
                                            '</div>' +
                                            '<div class="weui-cell weui-cell_link">' +
                                            '<div class="weui-cell__bd"></div>';
                                            if(orderlist[i].refund == 1) {
                                                div += '<div class="weui-cell__ft">' +
                                                        '<button type="button" class="btn btn-info btn-warning addcar">退款</button>' +
                                                        '</div>'+
                                                        '&nbsp;';
                                            }

                                            div += '<div class="weui-cell__ft">' +
                                            '<button type="button" class="btn btn-info btn-block addcar">订单详情</button>' +
                                            '</div>';
                                } else {
                                    div += '<div class="weui-cell weui-cell_link">' +
                                            '<div class="weui-cell__bd" style="color: #cc0000;">待支付</div>' +
                                            '<div class="weui-cell__ft">共 ' + orderlist[i].total + ' 杯，应支付 ' + orderlist[i].tprice + '</div>' +
                                            '</div>' +
                                            '<div class="weui-cell weui-cell_link">' +
                                            '<div class="weui-cell__bd"></div>' +
                                            '<div class="weui-cell__ft">' +
                                            '<button type="button" class="btn btn-warning btn-block addcar">取消订单</button>' +
                                            '</div>' +
                                            '&nbsp;' +
                                            '<div class="weui-cell__ft">' +
                                            '<button type="button" id="pay'+orderid+'" onclick="wxpay(\''+orderid+'\')" class="btn btn-success btn-block addcar">继续付款</button>' +
                                            '</div>';

                                    div +=  '<div style="display: none;">' +
                                            '<input type="text" id="total'+orderid+'" value="'+ orderlist[i].total +'">' +
                                            '<input type="text" id="tprice'+orderid+'" value="'+ orderlist[i].tprice +'">' +
                                            '<input type="text" id="acprice'+orderid+'" value="'+ orderlist[i].acprice +'">' +
                                            '</div>';
                                }
                                div += '</div>' +
                                        '</div>' +
                                        '</div>' +
                                        '</div>';
                                return div;
                            });
                        }
                    }

                } else {
                    alert(result.code, result.msg)
                }
            },
            error: function (result) {
                alert(result.code);
            }
        });
    });


    //调起微信支付
    var wxpay = function(oid) {
        var param = {};
        var goodsinfo = "";
        var total = $("#total"+oid).val();
        var tprice = $("#tprice"+oid).val();
        var acprice = $("#acprice"+oid).val();

        $('.price'+oid).each(function (index) {
            var counts = parseInt($(this).children('.counts').val() || "0");
            var cprice = $(this).children('.cprice').val();
            var price = $(this).children('.price').val();
            var pid = $(this).children('.pid').val();
            var productname = $(this).children('.productname').val();
            var description = $(this).children('.description').val();

            if (index == $(".price"+oid).length - 1) {
                goodsinfo += '{"counts":"' + counts + '","cprice":"' + cprice + '","price":"' + price + '","pid":"' + pid + '","productname":"' + productname + '","description":"' + description + '"}';
            } else {
                goodsinfo += '{"counts":"' + counts + '","cprice":"' + cprice + '","price":"' + price + '","pid":"' + pid + '","productname":"' + productname + '","description":"' + description + '"},';
            }
        });

        var goodsinfos = '[' + goodsinfo + ']';

        param.accesstoken = "${accesstoken}";
        param.goodsinfo = goodsinfos;
        param.total = total;
        param.tprice = tprice;
        param.acprice = acprice;
        param.paymode = "1";
        param.openid = "${openid}";
        param.machserial = "${mach}";
        param.orderid = oid;
        param.tradetype = "JSAPI";
        console.log(param);

        $.ajax({
            type: 'POST',
            url: '${url}/order/order.do',
            cache: false,
            dataType: 'json',
            data: param,
            success: function (result) {
                console.log(result);
                if (result.success == false) {
                    console.log(result);
                    $.toast(result.msg, "text");
                    setTimeout(function () {
                        window.location.href = '/index.do';
                    }, 2500);
                } else {
                    if (result.code == 0) {
                        wx.chooseWXPay({
                            timestamp: result.body.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                            nonceStr: result.body.nonceStr, // 支付签名随机串，不长于 32 位
                            package: result.body.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                            signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                            paySign: result.body.paySign, // 支付签名
                            success: function (res) {
                                var param = {};
                                param.openid = '${openid}';
                                param.accesstoken = '${accesstoken}';
                                param.mach = '${mach}';
                                param.orderid = result.orderid;
                                param.outtradeno = result.outtradeno;
                                param.status = "FINISH";
                                $.ajax({
                                    type: 'POST',
                                    url: '${url}/order/orderstate.do',
                                    cache: false,
                                    dataType: 'json',
                                    data: param,
                                    success: function (result) {
                                        localStorage.removeItem("list${mach}");
                                        $.toast("操作成功");
                                        setTimeout(function () {
                                            var param = {};
                                            param.openid = '${openid}';
                                            param.accesstoken = '${accesstoken}';
                                            param.mach = '${mach}';
                                            $.submitFormPost('/myorder.do', param, 'POST');
                                        }, 1000);
                                    },
                                    error: function (result) {
                                        alert(result.code);
                                    }
                                });
                            },
                            fail:function(res){

                            },
                            error: function (res) {
                                var param = {};
                                param.openid = '${openid}';
                                param.accesstoken = '${accesstoken}';
                                param.mach = '${mach}';
                                param.orderid = result.orderid;
                                param.outtradeno = result.outtradeno;
                                param.status = "FAIL";
                                $.ajax({
                                    type: 'POST',
                                    url: '${url}/order/orderstate.do',
                                    cache: false,
                                    dataType: 'json',
                                    data: param,
                                    success: function (result) {
                                        $.toast("支付失败", "forbidden");
                                        setTimeout(function () {
                                            $(".orderBoxs").remove();
                                            var param = {};
                                            param.openid = '${openid}';
                                            param.accesstoken = '${accesstoken}';
                                            param.mach = '${mach}';
                                            $.submitFormPost('/myorder.do', param, 'POST');
                                        }, 1000);
                                    },
                                    error: function (result) {
                                        alert(result.code);
                                    }
                                });
                            },
                            cancel: function (res) {
                                var param = {};
                                param.openid = '${openid}';
                                param.accesstoken = '${accesstoken}';
                                param.mach = '${mach}';
                                param.orderid = result.orderid;
                                param.outtradeno = result.outtradeno;
                                param.status = "UNPAID";
                                $.ajax({
                                    type: 'POST',
                                    url: '${url}/order/orderstate.do',
                                    cache: false,
                                    dataType: 'json',
                                    data: param,
                                    success: function (result) {
                                        $.submitFormPost('/myorder.do', param, 'POST');
                                    },
                                    error: function (result) {
                                    }
                                });
                            }
                        });
                    } else {
                        $.alert(result.msg, "警告！");
                        //localStorage.removeItem('list');
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('连接错误');
            }
        });
    };


    var MAX = 99, MIN = 1;


    //返回
    $('#back').click(function () {
        var param = {};
        param.openid = '${openid}';
        param.accesstoken = '${accesstoken}';
        param.state = '${mach}';
        $.submitFormPost('/weixin.do', param, 'POST');
    });

    $("#main").on("click", function () {
        var param = {};
        param.openid = '${openid}';
        param.accesstoken = '${accesstoken}';
        param.state = '${mach}';
        $.submitFormPost('/weixin.do', param, 'POST');
    });

    $("#my").on("click", function () {
        var param = {};
        param.openid = '${openid}';
        param.accesstoken = '${accesstoken}';
        param.mach = '${mach}';
        $.submitFormPost('/my.do', param, 'POST');
    });
</script>
</body>
</html>